package com.jstyle.jclifexd.view;


import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.jstyle.jclifexd.utils.ScreenUtils;

import java.text.NumberFormat;
import java.util.Locale;

/**
 * 圆形进度条
 * 
 * @author Administrator
 *
 */
public class GymCircleProgress extends View {
	private String titleText;
	private  float circleRadiusCenter;
	private Paint progressPaint;//进度条画笔

	private int strokeWidth;

	private RectF rectF;
	private float endAngel = 90;
	private float[]endAngelArray=new float[]{0.0f,0.0f,0.0f};
	float startAngle=-90;



	private Paint bgCiclePaint;//背景圆画笔



	private int defaultWidth;
	private int defaultHeight;

	private int padding;
	private int progressColor= Color.parseColor("#ff9f19");

	int[]colorArray=new int[]{Color.parseColor("#e9603c"),Color.parseColor("#b5bd00"),Color.parseColor("#4aa7f9")};
	public GymCircleProgress(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		//defaultTitleText=getContext().getString(R.string.Steps);
		//TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.mCicleProgress);
		// titleText=array.getString(R.styleable.mCicleProgress_titleText);
		//float textSizeDimension=  array.getInt(R.styleable.mCicleProgress_centerTextSize,24);
		strokeWidth = ScreenUtils.dip2px(context, 12);

		circleRadiusCenter = ScreenUtils.dip2px(context, 60);
		//textSize = ScreenUtils.dip2px(context, textSizeDimension);

		padding=ScreenUtils.dip2px(context, 20);
		defaultHeight = (int) ((circleRadiusCenter+padding*2+strokeWidth)*2);
		defaultWidth =defaultHeight;

		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		//numberFormat.setMinimumFractionDigits(0);
		rectF = new RectF();
		progressPaint = new Paint();
		progressPaint.setColor(progressColor);
		progressPaint.setStyle(Paint.Style.STROKE);
		progressPaint.setAntiAlias(true);
		progressPaint.setStrokeWidth(strokeWidth);
		progressPaint.setStrokeCap(Paint.Cap.ROUND);



		bgCiclePaint = new Paint();
		bgCiclePaint.setColor(Color.parseColor("#002f3f"));
		bgCiclePaint.setAntiAlias(true);
		bgCiclePaint.setStyle(Paint.Style.STROKE);
		bgCiclePaint.setStrokeWidth(strokeWidth);
		bgCiclePaint.setStrokeCap(Paint.Cap.ROUND);


	}

	private String topTxt = "步数";
	private String bottomTxt = "0";

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		for(int i=0;i<3;i++){
			float radius=circleRadiusCenter+i*padding;
			rectF.set(getWidth()/2-radius, getHeight()/2-radius, getWidth()/2+radius, getHeight()/2+radius);
			progressPaint.setColor(colorArray[i]);
			drawBgArc(canvas,radius);
			drawProgress(canvas,rectF,i);
		}

	//	drawText(canvas);

	}

	private static final String TAG = "ActivityHistoryCirclePr";




	/**进度条
	 * @param canvas
	 * @param rectF
	 */
	private void drawProgress(Canvas canvas, RectF rectF,int position) {
		progressPaint.setStyle(Paint.Style.STROKE);
		canvas.drawArc(rectF, startAngle, endAngelArray[position], false, progressPaint);
	}

	/**背景圆
	 * @param canvas
	 * @param Radius
	 */
	private void drawBgArc(Canvas canvas,float Radius) {
		bgCiclePaint.setStyle(Paint.Style.STROKE);

		canvas.drawCircle(getWidth()/2,getHeight()/2,Radius,bgCiclePaint);
	//	canvas.drawArc(rectF, startAngle, 270, false, bgCiclePaint);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
			height = defaultHeight;
		}
		if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST) {
			width = defaultWidth;
		}
		setMeasuredDimension(width, height);

	}


	public void setProgress(float progress) {
		endAngel = 360f * progress;
		postInvalidate();
	}
	public void setProgressCal(float progress) {
		endAngelArray[0] = 360f * progress;
		postInvalidate();
	}
	public void setProgressTime(float progress) {
		endAngelArray[1] = 360f * progress;
		postInvalidate();
	}
	public void setProgressHeart(float progress) {
		endAngelArray[2] = 360f * progress;
		postInvalidate();
	}
	public void setProgressSleep(float progress) {
		endAngel = 360f * progress;
		if(endAngel>360)endAngel=360;
		postInvalidate();
	}

	private float start = 0.0f;
	float lastStep;
	public void startAnim(float end) {
		if (start == end)
			return;
		ObjectAnimator animator = ObjectAnimator.ofFloat(this, "progress",
				start, end);
		start = end;
		AnimatorSet animatorSet = new AnimatorSet();
		animatorSet.setDuration(300);
		animatorSet.setInterpolator(new DecelerateInterpolator());
		animatorSet.setTarget(this);
		animatorSet.play(animator);
		animatorSet.start();

	}
	public void startAnimSleep(String text, float end) {
		if (start == end)
			return;

		this.bottomTxt=text;
		ObjectAnimator animator = ObjectAnimator.ofFloat(this, "progressSleep",
				start, end);
		start = end;

		AnimatorSet animatorSet = new AnimatorSet();
		animatorSet.setDuration(300);
		animatorSet.setInterpolator(new DecelerateInterpolator());
		animatorSet.setTarget(this);
		animatorSet.play(animator);
		animatorSet.start();

	}





	public void update(int step,float goal) {
		float progress=goal/100f;
		this.bottomTxt = String.valueOf(step);
		startAnim(progress);
		//postInvalidate();
	}





	
}
